import { ref, reactive, onMounted, watch, inject } from 'vue'
import { EditType } from '@/type/BaseEnum'
import useInstance from '@/hooks/useInstance'
import { useRouter, useRoute } from 'vue-router'
import {
	getOrderListApi,
	getSelectGroupListApi,
	changeSelfPrimingApi,
	confirmHarvestApi
} from '@/api/manage/index'
import { OrderParm, OrderType, OrderZiTipParam } from '@/api/manage/MangeModel'
import { orderStore } from '@/store/order'
import { Toast } from '@nutui/nutui'
import useCommonStore from '@/store/common'

import {
	getRecentDay_Date,
	getRecentMonth_Date,
	HXtimeSlotChange
} from '@/utils/utils'
import { isRes } from '@/utils/pub-use'
export default function useAddress() {
	//新增弹框的ref属性
	const addRef = ref<{ show: (type?: string, row?: any) => void }>()
	const useRef = ref<{ show: (type?: string, row?: any) => void }>()
	const kuidiRef = ref<{ show: (type?: string, row?: any) => void }>()
	const commonStore = useCommonStore()
	const router = useRouter()
	const route = useRoute()
	const uOrderStore = orderStore()
	const sendsGoodStatus = ref(0)
	const getGoodStatus = ref(4)
	const serviceStatus = ref(90)
	const fixeHeight = ref()
	const tableHeight = ref()
	const hasMore = ref(true)
	const order_id = ref<number | string>()
	//表格数据
	const listData = reactive({
		list: [] as any,
		url: ''
	})
	//列表查询的参数
	const listParm = reactive<OrderParm>({
		phone: undefined,
		groupbuy_id: undefined,
		start_time: undefined,
		end_time: undefined,
		time: getRecentMonth_Date(6),
		status: undefined,
		status_type: undefined,
		pagesize: 10,
		page: 1,
		count: undefined,
		type: 1 // 团长
	})
	//获取全局属性
	const { global } = useInstance()
	//新增
	const addBtn = (n: any) => {
		addRef.value?.show(EditType.ADD)
	}
	//编辑
	const editBtn = (row: any) => {
		order_id.value = row.order_id
		addRef.value?.show(EditType.EDIT, row)
	}
	// 待发货选项
	const sendGoodOptions = ref([
		{
			type: 0,
			title: '待发货'
		},
		{
			type: 1,
			title: '已发货'
		},
		{
			type: 3,
			title: '已收货'
		}
	])

	// 待发货点击
	const sendsChoseBtn = (n: { type: number; title: string }) => {
		hasMore.value = true
		sendsGoodStatus.value = n.type
		listData.list = []
		listParm.page = 1
		listParm.status_type = n.type
		getList(listParm)
	}
	// 提货选项
	const getGoodOptions = ref([
		{
			type: 4,
			title: '待提货'
		},
		{
			type: 2,
			title: '已提货'
		}
	])
	// 待发货点击
	const getChoseBtn = (n: { type: number; title: string }) => {
		hasMore.value = true
		getGoodStatus.value = n.type
		listData.list = []
		listParm.page = 1
		listParm.status_type = n.type

		getList(listParm)
	}
	//售后
	const serviceOptions = ref([
		{
			type: 90,
			title: '待退款'
		},
		{
			type: 99,
			title: '已退款'
		},
		{
			type: 98,
			title: '已取消'
		},
		{
			type: 100,
			title: '拒绝退款'
		}
	])
	//
	const serviceChoseBtn = (n: { type: number; title: string }) => {
		hasMore.value = true
		listData.list = []
		listParm.page = 1
		serviceStatus.value = n.type
		listParm.status = n.type
		getList(listParm)
	}
	const tabs = reactive<any>({
		options1: [{ text: '全部商品', value: undefined }],
		options2: [
			{ text: '近6个月', value: getRecentMonth_Date(6) },
			{ text: '本日', value: getRecentDay_Date(0) },
			{ text: '昨日', value: getRecentDay_Date(-1) },
			{ text: '本周', value: getRecentDay_Date(-6) },
			{ text: '近两周', value: getRecentDay_Date(-14) },
			{ text: '本月', value: HXtimeSlotChange('本月').startTime }
		],
		groupbuy_id: undefined,
		time: getRecentMonth_Date(6)
	})
	const state = reactive({
		tabvalue: 0,
		list: [
			{
				type: 0,
				title: '全部'
			},
			{
				type: 1,
				title: '发货'
			},
			{
				type: 2,
				title: '提货'
			},
			{
				type: 3,
				title: '售后'
			}
		]
	})
	const getList = async (parm: OrderParm) => {
		let res = await getOrderListApi(parm)
		if (res && res.code == 0) {
			const { list, count, url } = res.result
			listData.url = commonStore.img_domain
			if (list && list.length <= 0) {
				hasMore.value = false
				return
			}
			list.map((item: any) => {
				item.type = 1
			})
			listData.list = listData.list.concat(list)
			listParm.count = count
		}
	}
	const getTabs = async () => {
		const { groupbuy_id } = route.query
		let res = await getSelectGroupListApi()
		if (res && res.code == 0) {
			const { list } = res.result
			if (list && list.length) {
				list.map((item: any) => {
					item.text = item.title
					item.value = item.id
					tabs.options1.push(item)
				})
				if (groupbuy_id) {
					tabs.groupbuy_id = (groupbuy_id as any) * 1
					listParm.groupbuy_id = tabs.groupbuy_id
					getList(listParm)
				} else {
					getList(listParm)
				}
			}
		}
	}
	const handleChangeTabs0 = (n: any) => {
		listData.list = []
		listParm.page = 1
		listParm.groupbuy_id = n
		getList(listParm)
	}
	const handleChangeTabs1 = (n: any) => {
		listData.list = []
		listParm.page = 1
		listParm.time = n
		getList(listParm)
	}
	const changeState = (n: any) => {
		hasMore.value = true
		listData.list = []
		listParm.page = 1
		if (n.paneKey == 0) {
			//全部
			listParm.status = undefined
			listParm.status_type = undefined
		}
		if (n.paneKey == 1) {
			//发货
			listParm.status = 1
			listParm.status_type = sendsGoodStatus.value
		}
		if (n.paneKey == 2) {
			//提货
			listParm.status = 1
			listParm.status_type = getGoodStatus.value
		}
		if (n.paneKey == 3) {
			//售后
			listParm.status = serviceStatus.value
			listParm.status_type = undefined
		}
		getList(listParm)
	}
	const reFresh = () => {
		listParm.page = 1
		listData.list = []
		getList(listParm)
	}
	const loadMore = async (done: any) => {
		listParm.page = (listParm.page as number) * 1 + 1
		await getList(listParm)
		done()
	}
	const handleSearch = (n: any) => {
		reFresh()
	}
	const save = (n: OrderType) => {
		n.type = 1
		uOrderStore.setorderItem(n)
		router.push({
			path: '/ordermanageinfo'
		})
	}
	const hasSave = async (n: any) => {
		let res: any = await changeSelfPrimingApi({
			order_id: order_id.value,
			self_priming_id: n.currentSelect
		} as OrderZiTipParam)

		if (res && res.code == 0) {
			// reFresh()
			Toast.success('修改成功')
		}
	}
	// 编辑用户
	//新增
	const addUseBtn = (n: any) => {
		useRef.value?.show(EditType.ADD)
	}
	//编辑
	const editUseBtn = (row: any) => {
		// order_id.value = row.order_id
		useRef.value?.show(EditType.EDIT, row)
	}
	//编辑
	const kuiBtn = (row: any) => {
		kuidiRef.value?.show(EditType.EDIT, row)
	}

	//确认核销 1.团员确认收货  2.团长核销
	const confirm = async (item:any)=>{
		let res = await confirmHarvestApi({order_id:item.order_id,type:2})
		if(isRes(res)){
			reFresh()
		}
		
	}
	//团长退款
	const refound = (item:any)=>{
		router.push({path:'/refound',query:{
			order_id:item.order_id
		}})
	}
	onMounted(() => {
		getTabs()
		// getList(listParm)
		tableHeight.value = fixeHeight.value.offsetHeight
	})

	return {
		kuidiRef,
		listData,
		getList,
		addRef,
		addBtn,
		editBtn,
		reFresh,
		sendGoodOptions,
		sendsGoodStatus,
		sendsChoseBtn,
		getGoodOptions,
		getGoodStatus,
		getChoseBtn,
		serviceOptions,
		serviceChoseBtn,
		serviceStatus,
		tabs,
		listParm,
		handleChangeTabs0,
		handleChangeTabs1,
		state,
		changeState,
		loadMore,
		hasMore,
		handleSearch,
		save,
		tableHeight,
		fixeHeight,
		hasSave,
		useRef,
		addUseBtn,
		editUseBtn,
		kuiBtn,
		confirm,
		refound
	}
}
